<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Entry extends Public_Controller {
	
	public function __construct()
	{
		parent::__construct();
	}
	
	public function create()
	{
		// VALIDATION
		$this->load->library('form_validation');
		$this->form_validation->set_rules('title', 'Başlık', 'trim|required|xss_clean|min_length[10]|max_length[255]');	
		$this->form_validation->set_rules('body', 'İçerik', 'trim|required|xss_clean|min_length[10]|max_length[1000]|callback__check_login');	
		$this->form_validation->set_rules('target', 'Hedef', 'trim|required|xss_clean|min_length[3]|max_length[255]');
		
		if(!$this->form_validation->run())
		{
			ajax_response($this->form_validation->error_string());
		}
		
		$time = time();

		// CONTINUE
		$data = array( 
			'main' => array(
				'user_id' => $this->auth->uid(),
				'title' => $this->input->post('title'),
				'body' => $this->input->post('body'),
				'created_at' => $time
			),
			'target' => array(
				'name' => $this->input->post('target'),
				'created_at' => $time
			)
		);
		
		$this->entry_model->create($data);
		
		ajax_response('İşlem başarıyla tamamlandı', FALSE);
	}
	
	public function index()
	{
		// get rows
		$result = $this->entry_model->get_all();

		$data = array(
			'result' => $result
		);

		$this->template->load('entry/index', $data);
	}	

	public function detail($id)
	{
		$row = $this->entry_model->get($id);

		// gets comments of content and render it
		$comments = $this->comment_model->getsById($id);
		$comments = $this->template->load_partial('comment/comments_template', array('comments' => $comments), TRUE);

		//var_dump($comments);

		$data = array(
			'row' => $row,
			'comments' => $comments
		);

		$this->template->load('entry/detail', $data);
	}
	
	public function _check_login()
	{
		if($this->auth->is_logged_in() !== TRUE)
		{
			$this->form_validation->set_message('_check_login', 'Bu işlem için giriş yapmış olmanız gerekiyor.');
			return FALSE;
		}
		
		return TRUE;
	}
}

/* End of file entry.php */
